﻿using StudentWeb.BLL;
using StudentWeb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace StudentWeb.Api.Controllers
{
    public class BookController : ApiController
    {
        private BookBO bo = new BookBO();

        [HttpGet]
        public IHttpActionResult GetBookInfo(int bookId)
        {
            var result = bo.GetBookInfo(bookId);
            return Json(result);
        }

        [HttpPost]
        public IHttpActionResult AddBook(dynamic data)
        {
            var book = new BookData()
            {
                BookName = data.BookName,
                BookYear = data.BookYear,
            };
            var result = bo.InsertOne(book);
            return Json(result);
        }

        [HttpGet]
        public IHttpActionResult Delete(int bookId)
        {
            var result = bo.DeleteOne(bookId);
            return Json(result);
        }

        [HttpPost]
        public IHttpActionResult Update(dynamic data)
        {
            var book = new BookData()
            {
                Id = data.Id,
                BookName = data.BookName,
                BookYear = data.BookYear,
            };
            var result = bo.Update(book);
            return Json(result);
        }

        [HttpGet]
        public IHttpActionResult List()
        {
            var result = bo.List();
            return Json(result);
        }

        [HttpPost]
        public IHttpActionResult Save(dynamic data)
        {
            var book = new BookData()
            {
                Id = data.Id ?? 0,
                BookName = data.BookName,
                BookYear = data.BookYear,
            };
            var result = bo.Save(book);
            return Json(result);
        }
    }
}
